﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace Workflow.GetNum
{
    public partial class Detail : System.Web.UI.Page
    {
        private GetNumManager objGetNumManager = new GetNumManager();
        int workParentId;
        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            if (!IsPostBack)
            {
                try
                {
                    Models.GetNum gn = new Models.GetNum();
                    gn = objGetNumManager.GetInfo(workParentId);
                    if (gn != null)
                    {
                        ddlBusiness.SelectedValue = gn.Business;
                        txtZNum.Text = gn.ZNum;
                        txtMold.Text = gn.SapMold;
                        txtProject.Text = gn.SapProject;
                        txtOrderNo.Text = gn.OrderNumber;
                        ddlCost.SelectedValue = gn.CostCenter;
                        ddlBusiness.Enabled = false;
                    }
                    else
                    {
                        ddlBusiness.SelectedValue = "";
                        txtZNum.Text = "";
                        txtMold.Text = "";
                        txtProject.Text = "";
                        txtOrderNo.Text = "";
                        ddlCost.SelectedValue = "";
                        ddlBusiness.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Models.GetNum gn = new Models.GetNum();
                gn.Business = ddlBusiness.SelectedValue;
                gn.ZNum = txtZNum.Text;
                gn.SapMold = txtMold.Text.Trim().ToString();
                gn.SapProject = txtProject.Text.Trim().ToString();
                gn.OrderNumber = txtOrderNo.Text.Trim().ToString();
                gn.CostCenter = ddlCost.SelectedValue;
                gn.WorkParentId = workParentId;
                objGetNumManager.InsertNum(ref gn);
                ddlBusiness.Enabled = false;
                string msg = string.Format("<script>alert('提交成功')</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                ddlBusiness.SelectedValue = gn.Business;
                txtZNum.Text = gn.ZNum;
                txtMold.Text = gn.SapMold;
                txtProject.Text = gn.SapProject;
                txtOrderNo.Text = gn.OrderNumber;
                ddlCost.SelectedValue = gn.CostCenter;
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }
}